<?php

// --------------------------------------------------------
// [ 精确采集 ]
// --------------------------------------------------------
// use app\common\lib\ExactCollect as ExactCollectCommonLib;
// $res = ExactCollectCommonLib::run($no);
// if ($res['ret']) {
//     p($res['data']);
// } else {
//     // 采集失败
//     echo $res['msg'];
// }
// --------------------------------------------------------
// Array
// (
//     [ret] => 1
//     [data] => Array
//         (
//             [no] => 659681
//             [item] => Array
//                 (
//                     [0] => 06
//                     [1] => 03
//                     [2] => 01
//                     [3] => 09
//                     [4] => 08
//                     [5] => 07
//                     [6] => 04
//                     [7] => 05
//                     [8] => 10
//                     [9] => 02
//                 )
//             [time] => 1514971320
//         )
// )
// --------------------------------------------------------
namespace app\common\lib;

class ExactCollect
{
    // 采集页面
    private static $_url = 'http://www.bwlc.gov.cn/bulletin/prevtrax.html?num=';
    // 源数据
    private static $_sourceData;
    // 临时数据
    private static $_tempData;
    // 异常消息
    private static $_msg;
    // 返回结果
    private static $_result;

    public static function run($no)
    {
        // # 获取数据源
        // ------------------------------------------------------------------
        $bool = self::_getSourceData($no);
        if (!$bool) return ['ret'=>false,'msg'=>self::$_msg];
        // ------------------------------------------------------------------
        
        // # 解析期号、开奖结果、开奖时间
        // ------------------------------------------------------------------
        $bool = self::_analyResult();
        if (!$bool) return ['ret'=>false,'msg'=>self::$_msg];
        // ------------------------------------------------------------------

        return ['ret'=>true,'data'=>self::$_result];
    }

    // ----------------------------------------------------------------------

    // 获取源数据
    private static function _getSourceData($no)
    {
        self::$_url .= $no;
        self::$_sourceData = file_get_contents(self::$_url);
        if (!self::$_sourceData) {
            self::$_msg = '获取源数据失败';
            return false;
        }
        // echo self::$_sourceData;die;
        
        return true;
    }

    // 解析期号、开奖结果、开奖时间
    private static function _analyResult()
    {
        self::$_tempData = self::$_sourceData;
        $pattern = '/<div class="lott_cont">(.*)<div class="fc_fanye">/iUs';
        $num = preg_match($pattern,self::$_tempData,$matches);
        if (0 == $num) {
            self::$_msg = '解析开奖结果失败[1]';
            return false;
        }
        // print_r($matches[1]);die;

        self::$_tempData = $matches[1];
        $pattern = '/<tr class="">(.*)<\/tr>/iUs';
        $num = preg_match($pattern,self::$_tempData,$matches);
        if (0 == $num) {
            self::$_msg = '官方暂无该期开奖结果';
            return false;
        }
        // print_r($matches[1]);die;

        self::$_tempData = $matches[1];
        $pattern = '/<td>(.*)<\/td>/iUs';
        $num = preg_match_all($pattern,self::$_tempData,$matches);
        if (0 == $num) {
            self::$_msg = '解析开奖结果失败[3]';
            return false;
        }
        // print_r($matches[1]);die;

        self::$_tempData = $matches[1];
        self::$_result['no']   = self::$_tempData[0];
        self::$_result['item'] = explode(',',self::$_tempData[1]);
        self::$_result['time'] = strtotime(self::$_tempData[2]);
        // p(self::$_result);die;
        
        return true;
    }
}